package cn.zjutleo.village_server.service;

import cn.zjutleo.village_server.constants.MessageConstants;
import cn.zjutleo.village_server.dto.MessageDto;
import cn.zjutleo.village_server.entity.Message;
import cn.zjutleo.village_server.exception.apiException.daoException.SelectException;
import cn.zjutleo.village_server.mapper.MessageMapper;
import cn.zjutleo.village_server.service.converter.MessageEntityToVo;
import cn.zjutleo.village_server.vo.MessageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : Fu Lantian
 * @date : Created in 2021/11/17
 * @description: 消息业务类
 */
@Service
public class MessageService {

    @Resource
    private MessageMapper messageMapper;

    /**
     * 根据用户id获取用户所有消息
     *
     * @param userId 用户id
     * @return 消息VO列表
     */
    public List<MessageVo> getAllMessageByUserId(Integer userId) {
        List<Message> messages = messageMapper.selectMessageListByUserId(userId);
        return messages.stream()
                .filter(Objects::nonNull)
                .map(MessageEntityToVo.INSTANCES::toMessageVo)
                .collect(Collectors.toList());
    }

    /**
     * 根据用户id获取所有消息数量
     *
     * @param userId 用户id
     * @return 所有消息数量
     */
    public Integer getAllMessageAccountByUserId(Integer userId) {
        return messageMapper.selectAllMessageAccountByUserId(userId);
    }

    /**
     * 根据用户id与消息类型获取该类型未读消息数量
     *
     * @param userId 用户id
     * @param type 消息类型
     * @return 未读消息条数
     * @throws SelectException type参数取值错误
     */
    public Integer getNotReadMessageAccountByUserIdAndType(Integer userId, String type) throws SelectException {
        int currentType = MessageConstants.TYPE_PRIVATE;
        switch(type) {
            case "system": {
                currentType = MessageConstants.TYPE_SYSTEM;
                break;
            }
            case "private": {
                break;
            }
            default: {
                throw new SelectException("type参数取值错误");
            }
        }
        return messageMapper.selectNotReadMessageAccountByUserIdAndType(userId, currentType);
    }

    /**
     * 发送系统消息
     *
     * @param messageDto 消息DTO
     */
    public void sendSystemMessage(MessageDto messageDto) {
        Message message = new Message();
        message.setReceiveId(messageDto.getReceiveId());
        message.setSenderId(messageDto.getSenderId());
        message.setTitle(messageDto.getTitle());
        message.setContent(messageDto.getContent());
        message.setStatus(MessageConstants.STATUS_NOT_READ);
        message.setType(MessageConstants.TYPE_SYSTEM);
        message.setDate(new Timestamp(System.currentTimeMillis()));
        messageMapper.insertSelective(message);
    }

    /**
     * 发送私信
     *
     * @param messageDto 消息DTO
     */
    public void sendPrivateMessage(MessageDto messageDto) {
        Message message = new Message();
        message.setReceiveId(messageDto.getReceiveId());
        message.setSenderId(messageDto.getSenderId());
        message.setTitle(messageDto.getTitle());
        message.setContent(messageDto.getContent());
        message.setStatus(MessageConstants.STATUS_NOT_READ);
        message.setType(MessageConstants.TYPE_PRIVATE);
        message.setDate(new Timestamp(System.currentTimeMillis()));
        messageMapper.insertSelective(message);
    }

    /**
     * 根据消息id更改消息状态
     *
     * @param id 消息id
     */
    public void changeMessageStatusById(Integer id) {
        Message message = messageMapper.selectByPrimaryKey(id);
        message.setStatus(MessageConstants.STATUS_READ);
        messageMapper.updateByPrimaryKeySelective(message);
    }

}
